package testcase.iotest;

import card.Cardset;
import card.Card;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.TestCase;
import io.*;

public class ConsoleIOTest extends TestCase {
	private ConsoleIO io;
	private PrintStream console;

	public ConsoleIOTest() {
		console = System.out;
	}

	public void setUp() {
	}

	public void tearDown() {
		System.setOut(console);
	}

	public void testBasicInput() {
		String input = "HelloWorld";

		System.setIn(new ByteArrayInputStream(input.getBytes()));
		io = new ConsoleIO();

		String inputReadFromConsoleIO = io.getNextInput();
		assertEquals(input, inputReadFromConsoleIO);
	}

	public void testBasicOutput() {
		String output = "HelloWorld";

		// test print(String)
		ByteArrayOutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		io = new ConsoleIO();
		io.print(output);
		String outputReadFromConsoleIO = outContent.toString();
		assertEquals(output, outputReadFromConsoleIO);

		// test println(String)
		outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		io = new ConsoleIO();
		io.println(output);
		outputReadFromConsoleIO = outContent.toString();
		output += "\r\n";
		assertEquals(output, outputReadFromConsoleIO);

		// test println()
		outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		io = new ConsoleIO();
		io.println();
		outputReadFromConsoleIO = outContent.toString();
		output = "\r\n";
		assertEquals(output, outputReadFromConsoleIO);

		/*
		 * byte[] consoleBytes = outContent.toByteArray(); byte[] outputBytes =
		 * output.getBytes();
		 * 
		 * System.setOut(console);
		 * 
		 * for (int i=0; i<consoleBytes.length; i++) {
		 * assertEquals(outputBytes,consoleBytes);
		 * //System.out.println(consoleBytes[i]+ " " + outputBytes[i]);
		 * //System.out.println(consoleBytes[i]); }
		 */
	}

	public void testPrintCard() {
		// test printing a diamond 3 card
		Card card = new Card('d', '3');
		ByteArrayOutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		io = new ConsoleIO();

		io.print(card, 0);

		String output = "0.(d3) ";
		assertEquals(output, outContent.toString());

		// test print a null card
		outContent.reset();
		Card card2 = new Card();
		io.print(card2, 0);
		assertEquals("Card's data is not present.\r\n", outContent.toString());
	}

	public void testPrintCardset() {
		// test printing a cardset with h2 and c4
		Cardset cards = new Cardset(3);
		Card card1 = new Card('h', '2');
		Card card2 = new Card('c', '4');

		cards.add(card1);
		cards.add(card2);

		ByteArrayOutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		io = new ConsoleIO();

		io.print(cards);

		String output = "0.(h2) 1.(c4) \r\n";
		assertEquals(output, outContent.toString());

		// test printing a null cardset
		cards = new Cardset(3);
		outContent.reset();
		io.print(cards);
		assertEquals("no cards in Cardset\r\n", outContent.toString());
	}

}
